/*
 * @lc app=leetcode.cn id=82 lang=cpp
 *
 * [82] 删除排序链表中的重复元素 II
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution
{
public:
  ListNode* tmp;
  int cnt;

  ListNode* deleteDuplicates(ListNode* head)
  {
    this->cnt = 0;
    ListNode* dummy = new ListNode(101);
    this->tmp = dummy;
    ListNode* prev = dummy;
    Recursive(head, prev);
    return dummy->next;
  }

  void Recursive(ListNode* head, ListNode* prev)
  {
    if (head == nullptr) {
      if (cnt == 1)
        prev->next = tmp;
    }
    else {
      if (head->val == tmp->val)
        ++cnt;
      else {
        if (cnt == 1) {
          prev->next = tmp;
          prev = tmp;
          prev->next = nullptr;
        }
        tmp = head;
        cnt = 1;
      }
      Recursive(head->next, prev);
    }
  }
};
// @lc code=end
